﻿package com.designoutput.ui.controls
{
    import com.designoutput.enum.Direction;
    import com.designoutput.event.SelectEvent;
    import com.designoutput.icon.DropdownOver;
    import com.designoutput.ui.newControls.VScroller;
    import com.designoutput.utils.Globals;
    
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    public class Dropdown extends Sprite
    {
		private var _popupVisible:Boolean = false;
		private var _popup:Sprite;
		
        private var _contentWidth:int;
		private var _contentScroller:VScroller;
		private var _disabledOverlay:Sprite;
		
        protected var openButton:IconButton;
        protected var item:SelectItem;
		protected var textField:TextField;
		protected var content:Sprite;
		
        public var length:int = 0;        

        public function Dropdown(toatlWidth:int, contentWidth:int)
        {
            _contentWidth = contentWidth;
            _popup = new Sprite();
            content = new Sprite();
			
            openButton = new IconButton(new DropdownOver(0, 0));
            openButton.addEventListener(MouseEvent.MOUSE_DOWN, _openButton_mouseDownHandler, false, 0, true);
            openButton.x = toatlWidth - openButton.width;
            addChild(openButton);
			
            textField = new TextField();
            textField.addEventListener(MouseEvent.MOUSE_DOWN, _openButton_mouseDownHandler, false, 0, true);
            textField.defaultTextFormat = new TextFormat("Arial, Helvetica, San-serif", 12, 0, false, false, false, null, null, "left");
            textField.multiline = false;
            textField.selectable = false;
            textField.background = true;
            textField.backgroundColor = 0xFFFFFF;
            textField.border = true;
            textField.borderColor = 0x909090;
            textField.height = 16;
            textField.width = toatlWidth - openButton.width - 1;
            addChild(textField);
        }

        public function Set(strVal:String) : void
        {
            if (this.item != null)
            {
                this.item.Select(false);
            }
            var index:int = 0;
            while (index < this.content.numChildren)
            {
                
                this.item = this.content.getChildAt(index) as SelectItem;
                if (this.item.name == strVal)
                {
                    break;
                }
				index++;
            }
			if(this.item)
			{
	            this.textField.text = this.item.text;
	            this.item.Select();
			}
        }

        public function disable(visible:Boolean = true) : void
        {
            if (this._disabledOverlay == null)
            {
                _disabledOverlay = new Sprite();
                _disabledOverlay.visible = false;
                _disabledOverlay.alpha = 0.3;
                _disabledOverlay.graphics.beginFill(9474192);
                _disabledOverlay.graphics.drawRect(0, 0, 10, 10);
                _disabledOverlay.graphics.endFill();
                _disabledOverlay.height = textField.height + 1;
                _disabledOverlay.width = textField.width + openButton.width + 1;
                _disabledOverlay.x = textField.x;
                _disabledOverlay.y = textField.y;
                addChild(_disabledOverlay);
            }
            _disabledOverlay.visible = visible;
        }

        protected function itemClick(evt:MouseEvent) : void
        {
            if (item != null)
                item.Select(false);
            
            item = evt.currentTarget as SelectItem;
            item.Select();
            textField.text = item.text;
			hidePopup();
            dispatchEvent(new SelectEvent(SelectEvent.COMMAND, item.name));
        }

        public function bind() : void
        {
            drawHolder();
        }

        private function _openButton_mouseDownHandler(evt:MouseEvent) : void
        {
            if(_popupVisible){
				hidePopup();
			} else {
				showPopup();
			}
        }

		private function item_overHandler(evt:MouseEvent) : void
        {
            dispatchEvent(new SelectEvent(SelectEvent.HOVER, (evt.currentTarget as SelectItem).name));
        }

        private function drawHolder() : void
        {			
            var _height:Number = Math.min(content.height, 150);
            _popup.graphics.beginFill(0x909090);
            _popup.graphics.drawRect(0, 0, _contentWidth, _height);
            _popup.graphics.beginFill(0xFFFFFF);
            _popup.graphics.drawRect(1, 1, _contentWidth - 2, _height - 2);
            _popup.graphics.endFill();
			
			_contentScroller = new VScroller();
			_contentScroller.y = 1;
			_contentScroller.width = _contentWidth;
			_contentScroller.height = _height;
			_contentScroller.setContent(content);
			_popup.addChild(_contentScroller);
        }

        public function get value() : String
        {
            return item != null ? (item.name) : ("");
        }
		
		public function get label() : String
		{
			return item != null ? (item.title.text) : ("");
		}

        public function addItem($title:String, $name:String, selected:Boolean = false) : void
        {
            var selectItem:* = new SelectItem($title, $name, _contentWidth);
            selectItem.addEventListener(MouseEvent.MOUSE_DOWN, itemClick);
            selectItem.addEventListener(MouseEvent.MOUSE_OVER, item_overHandler);
            selectItem.y = Math.round(content.height);
            selectItem.x = 1;
			
            content.addChild(selectItem);
			
            if (selected)
            {
                item = selectItem;
                selectItem.Select();
                textField.text = selectItem.text;
            }
            length +=1;
        }
		
		public function clearItems():void {
			while(content.numChildren > 0)
				content.removeChildAt(0);
		}
		
		private function clickOutsidePopup(evt:MouseEvent) : void
		{
			if (!_popup.hitTestPoint(stage.mouseX, stage.mouseY) && !openButton.hitTestPoint(stage.mouseX, stage.mouseY))
				hidePopup();
		}

		
		protected function showPopup():void {
			_popupVisible = true;
			Globals.popupManager.addPopup(_popup, false);
			var position:Point = Globals.popupManager.convertPosition(this, new Point(0, 16));
			_popup.x = position.x;
			_popup.y = position.y;		
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, clickOutsidePopup, true);
		}
		
		protected function hidePopup() : void
		{
			_popupVisible = false;
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, clickOutsidePopup, true);
			Globals.popupManager.removePopup(_popup);
		}
    }
}
